package com.bdqn.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.dao.AdminUserMapper;
import com.bdqn.bookshop.entity.AdminUser;
import com.bdqn.bookshop.service.AdminUserService;
import com.bdqn.bookshop.utils.MD5Util;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.utils.ResultType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 15:58
 **/
@Service
@Transactional
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {

    @Override
    public Result<AdminUser> getAdminUser(AdminUser adminUser) {
        QueryWrapper<AdminUser> qw = new QueryWrapper<AdminUser>();
        qw.eq(StringUtils.isBlank(adminUser.getAdminName()),"admin_name",adminUser.getAdminName());
        String pwd= MD5Util.MD5Encode(adminUser.getPassword(),"utf-8");
        qw.eq(StringUtils.isBlank(pwd),"password",pwd);
        qw.eq("islocked",1);
        AdminUser user =  baseMapper.selectOne(qw);
        if(user!=null){
            return Result.createTrueResult(ResultType.LOGIN,user,"登陆成功");
        }
        return Result.createError("登录失败,用户名或密码错误");
    }

    @Override
    public Result isPasCorrect(Integer id, String pass) {

        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<AdminUser>();
        queryWrapper.eq(id!=null,"id",id);
        String pwd= MD5Util.MD5Encode(pass,"utf-8");
        queryWrapper.eq(StringUtils.isNotBlank(pwd),"password",pwd);
        if(baseMapper.selectOne(queryWrapper)!=null){
            return Result.createSuccess("原密码正确");
        }
        return Result.createError("原密码错误");

    }

    @Override
    public Result updateName(AdminUser adminUser) {
        if(baseMapper.updateById(adminUser)>0){
            return  Result.createSuccess("修改成功,请重新登录");
        }
        return Result.createSuccess("修改失败,服务器异常");
    }

    @Override
    public Result updatePass(Integer id, String orPass, String newPass, String confirmPass) {
        if(!newPass.equals(confirmPass)){
            return Result.createError("两次密码不一致");
        }
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<AdminUser>();
        queryWrapper.eq(id!=null,"id",id);
        String pwd= MD5Util.MD5Encode(orPass,"utf-8");
        queryWrapper.eq(StringUtils.isBlank(pwd),"password",pwd);
        AdminUser adminUser = baseMapper.selectOne(queryWrapper);
        if(adminUser!=null){
            pwd= MD5Util.MD5Encode(confirmPass,"utf-8");
            adminUser.setPassword(pwd);
            if(baseMapper.updateById(adminUser)>0){
                return  Result.createSuccess("修改成功,请重新登录");
            }
        }else {
            return Result.createError("原密码错误");
        }
        return Result.createSuccess("修改失败,服务器异常");
    }
}
